package fr.resasante.ejb.service;

import java.util.Locale;

import javax.ejb.Stateless;

import fr.resasante.ejb.dto.User;
import fr.resasante.ejb.service.helper.EmailHelper;
import fr.resasante.ejb.service.helper.UserCreateHelper;
import fr.resasante.ejb.service.helper.UserUpdateHelper;
import fr.resasante.ejb.service.helper.UserViewHelper;
import fr.resasante.ejb.utils.EjbResponse;

/**
 * 
 * 
 * 
 * @author Gaël de Villeblanche
 */

@Stateless
public class UserService implements UserServiceRemote, UserServiceLocal {
	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * fr.resasante.ejb.service.UserServiceRemote#addPatient(fr.resasante.ejb
	 * .dto.User)
	 */
	@Override
	public EjbResponse addPatient(User userIncoming, Locale locale) {
		final EjbResponse ejbResponse = UserCreateHelper.getInstance()
				.addPatient(userIncoming);
		if (ejbResponse.getErrors().isEmpty()) {
			EmailHelper.getInstance().sendConfirmationAccountEmail(userIncoming, locale);
		}
		return ejbResponse;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * fr.resasante.ejb.service.UserServiceRemote#addProfessional(fr.resasante
	 * .ejb.dto.User)
	 */
	@Override
	public EjbResponse addProfessional(User userIncoming, Locale locale) {
		final EjbResponse ejbResponse = UserCreateHelper.getInstance().addProfessional(
				userIncoming);
		if (ejbResponse.getErrors().isEmpty()) {
			EmailHelper.getInstance().sendConfirmationAccountEmail(userIncoming, locale);
		}
		return ejbResponse;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * fr.resasante.ejb.service.UserServiceRemote#confirmUser(java.lang.String)
	 */
	@Override
	public EjbResponse confirmUser(String salt) {
		return UserUpdateHelper.getInstance().confirmUser(salt);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * fr.resasante.ejb.service.UserServiceRemote#getUserBySalt(java.lang.String
	 * )
	 */
	@Override
	public User getUserBySalt(String salt) {
		return UserViewHelper.getInstance().getUserBySalt(salt, true);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * fr.resasante.ejb.service.UserServiceRemote#getUserByUsername(java.lang
	 * .String)
	 */
	@Override
	public User getUserByUsername(String username) {
		return UserViewHelper.getInstance().getUserByUsername(username);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * fr.resasante.ejb.service.UserServiceRemote#retrieveAccount(java.lang.
	 * String, java.util.Locale)
	 */
	@Override
	public EjbResponse retrieveAccount(String mail, Locale locale) {
		return UserViewHelper.getInstance().retrieveAccount(mail, locale);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * fr.resasante.ejb.service.UserServiceRemote#updateUser(fr.resasante.ejb
	 * .dto.User)
	 */
	@Override
	public boolean changePassword(User user) {
		return UserUpdateHelper.getInstance().changePassword(user);
	}

	/* (non-Javadoc)
	 * @see fr.resasante.ejb.service.UserServiceRemote#changeAddress(fr.resasante.ejb.dto.User)
	 */
	@Override
	public boolean changeAddress(User user) {
		return UserUpdateHelper.getInstance().changeAddress(user);
	}
}
